using Assets.Scripts;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;


public class DataManager : Singleton<DataManager>
{
    //public TextAsset cardData;
    //List<CharacterCards> cards = new List<CharacterCards>();

    public string DataPath;
    public Dictionary<int, CharacterCardsDefine> characterCards = null;     //定义一个字典来存储卡牌信息

    public DataManager()
    {
        this.DataPath = Application.persistentDataPath + "/CharacterCardsDefine.json";
        Debug.LogFormat("DataManager > DataManager()");
        
    }
    private void Start()
    {
        LoadData();
    }

    public IEnumerator LoadData()   //在连接成功后开始加载卡牌信息
    {
        #region 暂时停用
        /*string[] dataRow = cardData.text.Split('\n');   //把数据按行分割
        foreach (var row in dataRow)    //逐行读取
        {
            string[] rowArray = row.Split(',');     //按逗号分割，代表每一个单元格内的内容
            if (rowArray[0] == "#")
                continue;
            else if (rowArray[0] == "Character")    //新建角色卡
            {
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                int maxHp = int.Parse(rowArray[3]);
                int speed = int.Parse(rowArray[4]);
                int qi = int.Parse(rowArray[5]);
                string skill = rowArray[6];

                CharacterCards characterCards = new CharacterCards(id, name, maxHp, speed, qi, skill);
                cards.Add(characterCards);
                Debug.Log("读取到角色卡：" + characterCards.name);
            }
        }*/
        #endregion

        //string json = File.ReadAllText(this.DataPath + "CharacterCardsDefine.txt");
        File.WriteAllText(DataPath, CharacterCardsDefine.characterData);
        string characterData = File.ReadAllText(DataPath);
        this.characterCards = JsonConvert.DeserializeObject<Dictionary<int, CharacterCardsDefine>>(characterData);
        
        yield return null;
    }

}



